﻿using MudEngine.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MudEngine.Collections
{
    public class ValueArray : List<Value>
    {
        public ValueArray(int capacity)
            : base(capacity)
        {
        }

        public bool Equals(ValueArray other)
        {
            if (this == other)
                return true;
            if (other == null)
                return false;
            if (this.Count != other.Count)
                return false;

            var comparer = EqualityComparer<Value>.Default;

            for (int i = 0; i < this.Count; i++)
            {
                Value secondValue = other[i];
                if (!comparer.Equals(this[i], secondValue))
                    return false;
            }
            return true;
        }

        internal Value Subarray(int startIndex, int endIndex)
        {
            ValueArray newArray = new ValueArray(endIndex - startIndex + 1);
            for (int i = startIndex; i <= endIndex && i < this.Count; i++)
                newArray.Add(this[i]);
            return newArray;
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder("({");
            bool first = true;
            foreach (Value val in this)
            {
                if (!first) builder.Append(", ");

                builder.Append(val.ToString());
                first = false;
            }
            builder.Append("})");
            return builder.ToString();
        }
    }
}
